import cv2
import numpy as np

# 读取图像
image = cv2.imread('zwy/2-1.jpg', cv2.IMREAD_GRAYSCALE)

# 使用高斯滤波平滑图像
blurred = cv2.GaussianBlur(image, (5, 5), 0)

# 使用Sobel算子计算图像梯度
sobelx = cv2.Sobel(blurred, cv2.CV_64F, 1, 0, ksize=5)
sobely = cv2.Sobel(blurred, cv2.CV_64F, 0, 1, ksize=5)

# 计算梯度的幅值和方向
magnitude = np.sqrt(sobelx**2 + sobely**2)
angle = np.arctan2(sobely, sobelx) * (180 / np.pi)

# 将梯度方向量化为指定数量的方向区间
num_bins = 8
angle = angle % 180
bin_size = 180 / num_bins
quantized = np.floor(angle / bin_size).astype(int)

# 构建直方图
histogram = np.zeros(num_bins)
for i in range(num_bins):
    histogram[i] = np.sum(magnitude[quantized == i])

# 归一化直方图
histogram /= np.sum(histogram)

# 输出指纹特征直方图
print(histogram)